class ListNode{
    public int data;
    public ListNode prev;
    public ListNode next;

    public ListNode(int data){
        this.data = data;
    }
}


public class doubleListLinked {
    public ListNode dummyHead;
    public ListNode last;
    public doubleListLinked(){
        this.dummyHead = new ListNode(-1);
    }

    public void display(){
        ListNode cur = this.dummyHead.next;
        while(cur != null){
            System.out.print(cur.data + " ");
            cur = cur.next;
        }
        System.out.println();
    }

    public int size(){
        ListNode cur = this.dummyHead.next;
        int count = 0;
        while(cur != null){
            count ++;
            cur = cur.next;
        }
        return count;
    }

    public void remove(int key){
        ListNode cur = this.dummyHead.next;
        while(cur != null){
            if(cur.data == key){
                if(cur == this.dummyHead.next){
                    this.dummyHead.next = this.dummyHead.next.next;
                    this.dummyHead.next.prev = this.dummyHead;
                }else{
                    cur.prev.next = cur.next;
                    if(cur.next != null){
                        cur.next.prev = cur.prev;
                    }else{
                        this.last = cur.prev;
                    }
                }
                return;
            }else{
                cur = cur.next;
            }
        }
    }

    public ListNode findIndex(int index){
        ListNode cur = dummyHead.next;
        while(index != 0){
            cur = cur.next;
            index--;
        }
        return cur;
    }

    public void addFirst(int data){
        ListNode node = new ListNode(data);
        if(this.dummyHead.next == null){
            node.prev = this.dummyHead;
            this.dummyHead.next = node;
            this.last = node;
            return;
        }else{
            node.next = this.dummyHead.next;
            node.prev = this.dummyHead;
            this.dummyHead.next.prev = node;
            this.dummyHead.next = node;
        }

    }

    public void addLast(int data){
        ListNode node = new ListNode(data);
        if(this.dummyHead.next == null && this.last == null){
            node.prev = this.dummyHead;
            this.last = node;
            return;
        }else{
            this.last.next = node;
            node.prev = this.last;
            this.last = node;
        }
    }

    public void addIndex(int index, int data){
        if(index < 0 || index > size()){
            System.out.println("位置不合法");
            return;
        }else{
            if(index == 0){
                addFirst(data);
                return;
            }
            if(index == size()){
                addLast(data);
            }else{
                ListNode node = new ListNode(data);
                ListNode cur;
                cur = findIndex(index);
                cur.prev.next = node;
                node.prev = cur.prev;
                cur.prev = node;
                node.next = cur;
            }
        }
    }
}
